package com.itheima.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.Code;
import com.itheima.common.MessageConstant;
import com.itheima.common.PageResult;
import com.itheima.common.Result;
import com.itheima.dto.RecycleDTO;
import com.itheima.service.BinService;
import com.itheima.vo.DishBinVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * author:zfm
 * des:bin controller
 */
@RestController
@RequestMapping("/bin")
public class BinController {

    @Autowired
    private BinService binService;

    /**
     * 回收站菜品查询
     * @param recycleDTO
     * @return
     */
    @PostMapping("/page")
    public PageResult pageBin(@RequestBody RecycleDTO recycleDTO) {
        Integer page = recycleDTO.getPage();
        Integer pageSize = recycleDTO.getPageSize();
        Integer isDeleted = recycleDTO.getIsDeleted();
        String name = recycleDTO.getName();
        LocalDateTime beginTime = recycleDTO.getBeginTime();
        LocalDateTime endTime = recycleDTO.getEndTime();
        Page<DishBinVo> dishBinPage = binService.binPage(page, pageSize, isDeleted, name, beginTime, endTime);
        if (dishBinPage!=null){
         return new PageResult(Code.OK, MessageConstant.DISH_GET_SUCCESS,dishBinPage.getRecords(),dishBinPage.getTotal());
        }else {
            return new PageResult(Code.ERROR,MessageConstant.DISH_GET_ERROR,null);
        }
    }

    /**
     * 回收站删除
     * @param ids
     * @return
     */
    @DeleteMapping("/delete")
    public Result binDelete(@RequestParam Long[] ids){
        int res = binService.binDelete(ids);
        if (res==ids.length){
            return new Result(Code.OK,MessageConstant.DISH_DELETE_SUCCESS);
        }else {
            return new Result(Code.ERROR,MessageConstant.DISH_DELETE_ERROR);
        }
    }

    /**
     * 回收站复原
     * @param ids
     * @return
     */
    @PutMapping("/status")
    public Result binStatus(@RequestParam Long[] ids){
        int res = binService.binStatus(ids);
        if (res==ids.length){
            return new Result(Code.OK,MessageConstant.DISH_UPDATE_SUCCESS);
        }else {
            return new Result(Code.ERROR,MessageConstant.DISH_UPDATE_ERROR);
        }
    }
}
